﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.IO.Ports;

namespace VIB.View
{
    /// <summary>
    /// Interaction logic for ReturnBookWindow.xaml
    /// </summary>
    public partial class GetInfo : Window
    {
        public int UserId { get; set; }
        public string Isbn { get; set; }

        [DefaultValue(false)]
        public bool ShowDatePicker { get; set; }

        public DateTime ReturnDate { get; set; }

        private static SerialDataReceivedEventHandler dataReceived;

        public GetInfo()
        {
            InitializeComponent();

            dataReceived = new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);

            Scanner.Instance.Port.DataReceived += dataReceived;
        }

        void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort com = (SerialPort)sender;

            string input = com.ReadExisting();
            input = input.Replace("\r\n", "");
            input = input.Trim();

            Dispatcher.BeginInvoke(new Action(() => textbox_isbn.Text += input));          
        }

        private void GetUserInput(object sender, RoutedEventArgs e)
        {          
            GetUserId promptUserId = new GetUserId();
            promptUserId.Owner = this;
            promptUserId.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            promptUserId.ShowDialog();

            if (promptUserId.DialogResult == true)
            {
                UserId = promptUserId.UserId;
                Isbn = textbox_isbn.Text;
                if (datepicker_returndate.SelectedDate != null)
                {
                    ReturnDate = (DateTime)datepicker_returndate.SelectedDate;
                }
               
                this.DialogResult = true;
                this.Close();
            }        
        }
        
       

        private void button_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (ShowDatePicker)
            {
                label_returndate.Visibility = System.Windows.Visibility.Visible;
                datepicker_returndate.Visibility = System.Windows.Visibility.Visible;                
                datepicker_returndate.DisplayDateStart = DateTime.Now;
            }
            else
            {
                label_returndate.Visibility = System.Windows.Visibility.Hidden;
                datepicker_returndate.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void datepicker_returndate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshControlStatus();
        }

        private void textbox_isbn_TextChanged(object sender, TextChangedEventArgs e)
        {
            RefreshControlStatus();
        }

        private void RefreshControlStatus()
        {
            if (textbox_isbn.Text != String.Empty && Regex.IsMatch(textbox_isbn.Text, @"^(97(8|9))?\d{9}(\d|X)$"))
            {
                if (datepicker_returndate.SelectedDate == null && ShowDatePicker)
                {
                    textblock_error.Text = "Bitte Rückgabedatum auswählen";
                }
                else
                {
                    textblock_error.Text = String.Empty;
                    button_returnBook.IsEnabled = true;
                }

            }
            else if (textbox_isbn.Text == String.Empty)
            {
                textblock_error.Text = "Bitte ISBN scannen";
                textblock_error.Foreground = Brushes.Green;
            }
            else
            {
                textblock_error.Text = "Keine gültige ISBN Nummer";
                textblock_error.Foreground = Brushes.Red;
                button_returnBook.IsEnabled = false;
            }
            if (ShowDatePicker)
            {
                datepicker_returndate.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void datepicker_returndate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                GetUserInput(sender, e);
            }
        }

        private void textbox_isbn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                GetUserInput(sender, e);
            }
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Scanner.Instance.Port.DataReceived -= dataReceived;
        }
    }
}
